﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpExample
{
    /* A collection is a set of similarly typed objects that are grouped together
     * 
     * The ICollection interface is the base interface for classes in the System.Collections namespace. 
     * The ICollection interface extends IEnumerable; IDictionary and IList are more specialized interfaces that extend ICollection. 
     * An IDictionary implementation is a collection of key/value pairs, like the Hashtable class. 
     * An IList implementation is a collection of values and its members can be accessed by index, like the ArrayList class. 
     * Some collections that limit access to their elements, such as the Queue class and the Stack class, directly implement the ICollection interface. 
     * If neither the IDictionary interface nor the IList interface meet the requirements of the required collection, 
     * derive the new collection class from the ICollection interface instead for more flexibility. 
     * 
     * All collections that directly or indirectly implement the ICollection interface or the ICollection<T>
     * generic interface share several features in addition to methods that add, remove, or search elements:
     * 
     * An enumerator.
     * An enumerator is an object that iterates through its associated collection. 
     * It can be thought of as a movable pointer to any element in the collection. A
     * n enumerator can be associated with only one collection, but a collection can have multiple enumerators. 
     * The C# foreach statement ( for each in Visual Basic) uses the enumerator and hides the complexity of manipulating the enumerator.
     * 
     * The CopyTo method.
     * All collections can be copied to an array using the CopyTo method; 
     * however, the order of the elements in the new array is based on the sequence in which the enumerator returns them.
     * The resulting array is always one-dimensional with a lower bound of zero.
     * 
     * Using generic collections is generally recommended, using generic collections is type safe and has better performance.
     */
    class ValueCollection<T> : ICollection<T>
    {
        private List<T> values;

        public ValueCollection()
        {
            values = new List<T>();
        }

        public T this[int index]
        {
            get { return values[index]; }
            set { values[index] = value; }
        }

        #region implementation of ICollection

        public int Count
        {
            get { return values.Count; }
        }

        public void Add(T item)
        {

            values.Add(item);
        }

        public void Clear()
        {
            values.Clear();
            
        }

        public bool Contains(T item)
        {
            return values.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            values.CopyTo(array, arrayIndex);
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public bool Remove(T item)
        {
            return values.Remove(item);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return values.GetEnumerator();
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return values.GetEnumerator();
        }
        #endregion


    }
}
